{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://mp.weixin.qq.com/s?__biz=MzkxODE3NjExOQ==&mid=2247485446&idx=1&sn=8f4f8d58c8921f2baff01f1ef8f59a0b&chksm=c1b4201ef6c3a908155629bbafd36533cfa92a6adc9c7e0d1685af66c6d667b3277d06d843a4&scene=178&cur_album_id=3354758663365918722#rd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "  胜率 = 盈利交易次数 / 总交易次数 ×100%  \n",
    "  赔率 = 平均每次盈利 / 平均每次亏损  \n",
    "  期望收益 = (胜率 × 平均每次盈利) - [(1 - 胜率) × 平均每次亏损]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入需要使用的库\n",
    "import akshare as ak\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 关闭警告信息\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "# 获取沪深300指数10年的收盘价数据\n",
    "start_date = '20140101'  # 开始日期\n",
    "end_date = '20231229'  # 结束日期\n",
    "bars = ak.stock_zh_index_hist_csindex(symbol='000300', start_date=start_date, end_date=end_date)\n",
    "prices = bars[['日期','收盘']]\n",
    "# 将日期设置为datetime格式\n",
    "prices['日期'] = pd.to_datetime(prices['日期'])\n",
    "prices = prices.set_index('日期')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算日收益率\n",
    "returns = prices['收盘'].pct_change()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 胜率 = 盈利交易次数 / 总交易次数\n",
    "win_rate = (returns > 0).sum() / returns.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 赔率 = 平均每次盈利 / 平均每次亏损\n",
    "avg_win = returns[returns > 0].mean()  # 平均每次盈利\n",
    "avg_loss = returns[returns < 0].mean()  # 平均每次亏损\n",
    "odds = abs(avg_win / avg_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 期望收益 = (胜率 × 平均每次盈利) - [(1 - 胜率) × 平均每次亏损]\n",
    "exp_returns = (win_rate * avg_win) - ((1 - win_rate) * abs(avg_loss))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "4broker",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
